package cn.elves.model.uua.mult.authenticator.password;


import cn.elves.model.entity.dto.UserDTO;
import cn.elves.model.provider.UserProvider;
import cn.elves.model.uua.mult.MultAuthentication;
import cn.elves.model.uua.mult.authenticator.AbstractPreparableMultAuthenticator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;


/**
 * 默认登录处理
 **/
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableMultAuthenticator {

    @Autowired
    private UserProvider userProvider;

    private final static String PASSWORD_AUTH_TYPE = "password";

    @Override
    public UserDTO authenticate(MultAuthentication multAuthentication) {
        //
        return userProvider.getUserByUsername(multAuthentication.getUsername()).getContent();
    }

    @Override
    public void prepare(MultAuthentication multAuthentication, HttpServletResponse response) {

    }

    @Override
    public boolean support(MultAuthentication multAuthentication) {
        return PASSWORD_AUTH_TYPE.equals(multAuthentication.getAuthType());
    }
}
